######### Notes ###########

# Demographics: All
#               Male, Female
#               16-24, 25-49, 50-64, 65+

# Other demographics not included
## Race (but non white categories very small n...)
## Class


######### BES Combination ###########

library(tidyverse)

read_csv("/Users/matteo/Desktop/Prison Polling/Prison Data Internship/Survey Data/Finished/BESmain_allrepeatedcrimequestions.csv") %>%
  mutate(deathpenBES = ifelse(death_penalty %in% c("support", "fairly impt should",
                                                     "v impt shd be done","agree","agree strongly",
                                                     "strongly agree"), 1,
                           ifelse(is.na(death_penalty), NA_integer_, 0)),
         stiffsentenceBES = ifelse(stiffsentences %in% c("fairly impt should","v impt shd be done",
                                                       "agree","agree strongly",
                                                      "strongly agree"), 1,
                                 ifelse(is.na(stiffsentences), NA_integer_, 0)),
         crim_imprt_vote = ifelse(crim_imprt_vote %in% c("fairly important", "extremely important",
                                                         "quite   importnt", "extremly importnt"), 1,
                                 ifelse(is.na(crim_imprt_vote), NA_integer_, 0)),
         crimerightscale = ifelse(crimerightscale == "-89", NA_integer_,
                                  ifelse(crimerightscale %in% c("7.5","10","7","8","5.5","6.5","8.5",
                                                               "9","6","9.5","11"), 1,
                                         ifelse(is.na(crimerightscale), NA_integer_, 0))),
         increasecrimeBES = ifelse(crimelevel %in% c("increased a lot", "increased little",
                                               "lot worse","little worse"), 1,
                                  ifelse(is.na(crimelevel), NA_integer_, 0)),
         violentcrimnorights = ifelse(violentcrimnorights %in% c("strongly agree",
                                               "agree","agree strongly"), 1,
                             ifelse(is.na(violentcrimnorights), NA_integer_, 0)),
         rehabcriminalsnotpunish = ifelse(rehabcriminalsnotpunish %in% c("disagree",
                                                                 "strongly disagree","disagree strongly"), 1,
                                      ifelse(is.na(rehabcriminalsnotpunish), NA_integer_, 0)),
         deathpenneverjust = ifelse(deathpenneverjust %in% c("disagree","strongly disagree","disagree strongly"), 1,
                                          ifelse(is.na(deathpenneverjust), NA_integer_, 0)),
         trstpol = ifelse(trstpol %in% c("10","9","8","7","6"), 1,
                          ifelse(is.na(trstpol), NA_integer_, 0)),
         trstcrts = ifelse(trstcrts %in% c(10:6), 1,
                          ifelse(is.na(trstcrts), NA_integer_, 0)),
         age = case_when(age > 15 & age < 25 ~ "16-24",    
                           age > 24 & age < 50 ~ "25-49",    
                           age > 49 & age < 65 ~ "50-64",    
                           age > 64 ~ "65+"),
         race = case_match(race, 
                           'white' ~ "White", 
                           c('asian','black','non-white','other') ~ "Non-white")) %>%
  select(gender, age, race, weight, year, deathpenBES, stiffsentenceBES, crim_imprt_vote,
                crimerightscale, increasecrimeBES, violentcrimnorights, rehabcriminalsnotpunish,
                deathpenneverjust, trstpol, trstcrts) %>%
  mutate(Date = paste0(year, "-01-01")) -> readforanalysis

readforanalysis %>%
  select(!year) %>%
  pivot_longer(-c(Date, gender, age, race, weight)) %>%
  drop_na(value, weight) %>%
  group_by(name, Date) %>%
  summarise(Index = weighted.mean(value, weight, na.rm = TRUE)) %>%
  mutate(Demographic = "All adults") %>%
  rename(Varname = name) %>%
  left_join(., readforanalysis %>%
              select(!year) %>%
              pivot_longer(-c(Date, gender, age, race, weight)) %>%
              drop_na(value, weight) %>%
              group_by(name, Date) %>%
              summarise(n = n()) %>%
              mutate(Demographic = "All adults") %>%
              rename(Varname = name)) %>%
  bind_rows(readforanalysis %>%
              select(!year) %>%
              pivot_longer(-c(Date, gender, age, race, weight)) %>%
              drop_na(value, weight, age) %>%
              group_by(name, Date, age) %>%
              summarise(Index = weighted.mean(value, weight, na.rm = TRUE)) %>%
              rename(Demographic = age) %>%
              rename(Varname = name) %>%
              left_join(., readforanalysis %>%
                          select(!year) %>%
                          pivot_longer(-c(Date, gender, age, race, weight)) %>%
                          drop_na(value, weight, age) %>%
                          group_by(name, Date, age) %>%
                          summarise(n = n()) %>%
                          rename(Demographic = age) %>%
                          rename(Varname = name))) %>%
  bind_rows(readforanalysis %>%
              select(!year) %>%
              pivot_longer(-c(Date, gender, age, race, weight)) %>%
              drop_na(value, weight, gender) %>%
              group_by(name, Date, gender) %>%
              summarise(Index = weighted.mean(value, weight, na.rm = TRUE)) %>%
              rename(Demographic = gender) %>%
              rename(Varname = name) %>%
              left_join(., readforanalysis %>%
                          select(!year) %>%
                          pivot_longer(-c(Date, gender, age, race, weight)) %>%
                          drop_na(value, weight, gender) %>%
                          group_by(name, Date, gender) %>%
                          summarise(n = n()) %>%
                          rename(Demographic = gender) %>%
                          rename(Varname = name))) %>%
  bind_rows(readforanalysis %>%
              select(!year) %>%
              pivot_longer(-c(Date, gender, age, race, weight)) %>%
              drop_na(value, weight, race) %>%
              group_by(name, Date, race) %>%
              summarise(Index = weighted.mean(value, weight, na.rm = TRUE)) %>%
              rename(Demographic = race) %>%
              rename(Varname = name) %>%
              left_join(., readforanalysis %>%
                          select(!year) %>%
                          pivot_longer(-c(Date, gender, age, race, weight)) %>%
                          drop_na(value, weight, race) %>%
                          group_by(name, Date, race) %>%
                          summarise(n = n()) %>%
                          rename(Demographic = race) %>%
                          rename(Varname = name))) %>%
  select(Varname, Date, Index, Demographic, n) %>%
  mutate(Poll = "BES Cross Sections") -> FinalCrossSectional



### Panels

read_csv("/Users/matteo/Desktop/Prison Polling/Prison Data Internship/Survey Data/Finished/BESpanels_allrepeatedcrimequestions.csv") %>%
  mutate(deathpenBES = ifelse(death_penalty %in% c("agree","agree strongly",
                                                     "strongly agree"), 1,
                                ifelse(is.na(death_penalty), NA_integer_, 0)),
         stiffsentenceBES = ifelse(stiffsentences %in% c("agree","agree strongly",
                                                       "strongly agree"), 1,
                                 ifelse(is.na(stiffsentences), NA_integer_, 0)),
         crimerightscale = ifelse(crimerightscale %in% c("7","10","5.5","9","6","6.5","8.5",
                                                                "7.5","8","9.5"), 1,
                                  ifelse(is.na(crimerightscale), NA_integer_, 0)),
         increasecrimeBES = ifelse(crimelevel %in% c("increasd a lot","increasd little",
                                               "increased a lot", "increased a little",
                                               "a lot worse","a little worse"), 1,
                             ifelse(is.na(crimelevel), NA_integer_, 0)),
         age = case_when(age > 15 & age < 25 ~ "16-24",    
                           age > 24 & age < 50 ~ "25-49",    
                           age > 49 & age < 65 ~ "50-64",    
                           age > 64 ~ "65+"),
         race = case_match(race, 'white' ~ "White",
                           c('asian','black','other') ~ "Non-white")) %>%
  select(gender, age, race, weight, year, deathpenBES, stiffsentenceBES,
         crimerightscale, increasecrimeBES, Emotion_Crime_Afraid, Emotion_Crime_Uneasy,
         Emotion_Crime_Angry, Emotion_Crime_Disgusted) %>%
  mutate(Date = ifelse(nchar(year) == 4, paste0(year, "-01-01"), paste0(year, "-01"))) -> panelsforanalysis

# Weighted score for each year by demographic characteristics

panelsforanalysis %>%
  select(!year) %>%
  pivot_longer(-c(Date, gender, age, race, weight)) %>%
  drop_na(value, weight) %>%
  group_by(name, Date) %>%
  summarise(Index = weighted.mean(value, weight, na.rm = TRUE)) %>%
  mutate(Demographic = "All adults") %>%
  rename(Varname = name) %>%
  left_join(., panelsforanalysis %>%
              select(!year) %>%
              pivot_longer(-c(Date, gender, age, race, weight)) %>%
              drop_na(value, weight) %>%
              group_by(name, Date) %>%
              summarise(n = n()) %>%
              mutate(Demographic = "All adults") %>%
              rename(Varname = name)) %>%
  bind_rows(panelsforanalysis %>%
              select(!year) %>%
              pivot_longer(-c(Date, gender, age, race, weight)) %>%
              drop_na(value, weight, age) %>%
              group_by(name, Date, age) %>%
              summarise(Index = weighted.mean(value, weight, na.rm = TRUE)) %>%
              rename(Demographic = age) %>%
              rename(Varname = name) %>%
              left_join(., panelsforanalysis %>%
                          select(!year) %>%
                          pivot_longer(-c(Date, gender, age, race, weight)) %>%
                          drop_na(value, weight, age) %>%
                          group_by(name, Date, age) %>%
                          summarise(n = n()) %>%
                          rename(Demographic = age) %>%
                          rename(Varname = name))) %>%
  bind_rows(panelsforanalysis %>%
              select(!year) %>%
              pivot_longer(-c(Date, gender, age, race, weight)) %>%
              drop_na(value, weight, gender) %>%
              group_by(name, Date, gender) %>%
              summarise(Index = weighted.mean(value, weight, na.rm = TRUE)) %>%
              rename(Demographic = gender) %>%
              rename(Varname = name) %>%
              left_join(., panelsforanalysis %>%
                          select(!year) %>%
                          pivot_longer(-c(Date, gender, age, race, weight)) %>%
                          drop_na(value, weight, gender) %>%
                          group_by(name, Date, gender) %>%
                          summarise(n = n()) %>%
                          rename(Demographic = gender) %>%
                          rename(Varname = name))) %>%
  bind_rows(panelsforanalysis %>%
              select(!year) %>%
              pivot_longer(-c(Date, gender, age, race, weight)) %>%
              drop_na(value, weight, race) %>%
              group_by(name, Date, race) %>%
              summarise(Index = weighted.mean(value, weight, na.rm = TRUE)) %>%
              rename(Demographic = race) %>%
              rename(Varname = name) %>%
              left_join(., panelsforanalysis %>%
                          select(!year) %>%
                          pivot_longer(-c(Date, gender, age, race, weight)) %>%
                          drop_na(value, weight, race) %>%
                          group_by(name, Date, race) %>%
                          summarise(n = n()) %>%
                          rename(Demographic = race) %>%
                          rename(Varname = name))) %>%
  select(Varname, Date, Index, Demographic, n) %>%
  mutate(Poll = "BES Panels") -> FinalPanel


### BESIP

read_csv("/Users/matteo/Desktop/Prison Polling/Prison Data Internship/Survey Data/Finished/BESIP_summaries.csv", col_types = cols(Date = col_character())) |>
  mutate(Date = paste0(substr(Date,1,4),'-01-01')) |>
  group_by(Date, Varname, Demographic) |>
  summarise(Index = weighted.mean(Index, n, na.rm = TRUE),
            n = sum(n)) |>
  mutate(Poll = "BES Internet Panel") -> BESIP
  

### Split and save

BESIP %>%
  drop_na(Index) %>%
  bind_rows(., FinalCrossSectional, FinalPanel) %>%
  filter(Varname %in% c("stiffsentBESIP", "stiffsentenceBES", "crimerightscale", "rehabcriminalsnotpunish",
                        "violentcrimnorights","Emotion_Crime_Disgusted","Emotion_Crime_Angry")) %>%
  write.csv(., "/Users/matteo/Desktop/Prison Polling/Dyad Ratio Ready/BES/Punitiveness.csv", row.names = FALSE)

BESIP %>%
  drop_na(Index) %>%
  bind_rows(., FinalCrossSectional, FinalPanel) %>%
  filter(Varname %in% c("deathpenBES","deathpenBSIP","deathpenneverjust")) %>%
  write.csv(., "/Users/matteo/Desktop/Prison Polling/Dyad Ratio Ready/BES/DeathPenalty.csv", row.names = FALSE)

BESIP %>%
  drop_na(Index) %>%
  bind_rows(., FinalCrossSectional, FinalPanel) %>%
  filter(Varname %in% c("increasecrimeBSIP","increasecrimeBES","Emotion_Crime_Afraid","Emotion_Crime_Uneasy")) %>%
  write.csv(., "/Users/matteo/Desktop/Prison Polling/Dyad Ratio Ready/BES/FearCrime.csv", row.names = FALSE)

BESIP %>%
  drop_na(Index) %>%
  bind_rows(., FinalCrossSectional, FinalPanel) %>%
  filter(Varname %in% c("trstcrts","trstpol")) %>%
  write.csv(., "/Users/matteo/Desktop/Prison Polling/Dyad Ratio Ready/BES/Satisfaction.csv", row.names = FALSE)

BESIP %>%
  drop_na(Index) %>%
  bind_rows(., FinalCrossSectional, FinalPanel) %>%
  filter(Demographic == "All adults") %>%
  filter(Varname %in% c("mii_crime","mii_health","mii_edu","mii_defence","crimepriority","crim_imprt_vote")) %>%
  pivot_wider(names_from = Varname, values_from = Index, values_fill = NA) %>%
  select(Date, mii_crime, mii_health, mii_edu, mii_defence, crimepriority, crim_imprt_vote, Poll) %>%
  mutate(Year = substr(Date,1,4)) %>%
  group_by(Year, Poll) %>%
  summarise(across(c(mii_crime, mii_health, mii_edu, mii_defence, crimepriority, crim_imprt_vote),
            \(x) mean(x, na.rm = T))) %>%
  select(Year, mii_crime, mii_health, mii_edu, mii_defence, crimepriority, crim_imprt_vote, Poll) %>%
  write.csv(., "/Users/matteo/Desktop/Prison Polling/MIP/MIP BES.csv", row.names = FALSE)

  